package com.cskaoyan.market.config;

import com.cskaoyan.market.TestVo.ApiMenuItem;
import com.cskaoyan.market.TestVo.MenuItem;
import com.cskaoyan.market.TestVo.MidMenuItem;
import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.util.Permission;
import com.cskaoyan.market.util.PermissionUtil;
import com.cskaoyan.market.util.ScanResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: chenjian
 * @Date: 2024/5/23 9:32
 */
@Configuration
public class MyPermissionConfig {
    @Autowired
    private ApplicationContext applicationContext;
    @Autowired
    private ScanResults scanResults;

    @Bean
    public ScanResults doScan() {
        // 这里是你的包名，可以硬编码或者从配置文件中读取
        String packageName = "com.cskaoyan.market.controller.Admin";
        // 调用你的扫描方法，并将结果保存到scanResults中
        List<Permission> permissionList = PermissionUtil.listPermission(applicationContext, packageName);

        ScanResults scanResults1 = new ScanResults();
/*        for (Permission permission : permissionList) {
            System.out.println("permission.toString() = " + permission.toString());
        }*/
        
        Map<String,List<Permission>> permissionMap = new HashMap<>();
        for (Permission permission : permissionList) {
            RequiresPermissionsDesc requiresPermissionsDesc = permission.getRequiresPermissionsDesc();
            String[] menu = requiresPermissionsDesc.menu();
            if(permissionMap.get(menu[0]) == null){
                ArrayList<Permission> permissions = new ArrayList<>();
                permissions.add(permission);
                permissionMap.put(menu[0],permissions);
            }else{
                List<Permission> permissions = permissionMap.get(menu[0]);
                permissions.add(permission);
                permissionMap.put(menu[0],permissions);
            }
        }


        for (Map.Entry<String, List<Permission>> stringListEntry : permissionMap.entrySet()) {
            List<Permission> value = stringListEntry.getValue();
            for (Permission permission : value) {
                List<MenuItem> menuItems = new ArrayList<MenuItem>();
                List<String> buttons=new ArrayList<>();
                RequiresPermissionsDesc requiresPermissionsDesc = permission.getRequiresPermissionsDesc();
                String[] menu = requiresPermissionsDesc.menu();
                String id =menu[1];
                String label = menu[1];
                String api = permission.getApi();
                ApiMenuItem apiMenuItem = new ApiMenuItem();
                apiMenuItem.setId(id);
                apiMenuItem.setLabel(label);
                apiMenuItem.setApi(api);

                MidMenuItem midMenuItem = new MidMenuItem();
                midMenuItem.setId(menu[0]);
                midMenuItem.setLabel(menu[0]);
                List<ApiMenuItem> apiMenuItemlist = new ArrayList<>();
                midMenuItem.setChildren(apiMenuItemlist);

                MenuItem topMenu = new MenuItem();
                topMenu.setId(menu[0]);
                topMenu.setLabel(menu[0]);
                List<MidMenuItem> midMenuItemlist = new ArrayList<>();
                midMenuItemlist.add(midMenuItem);
                topMenu.setChildren(midMenuItemlist);
                menuItems.add(topMenu);
                String button = requiresPermissionsDesc.button();
                buttons.add(button);
            }
            }
        return scanResults1;
        }
    }